package io.renren.modules.question.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.question.dao.CommonQuestionDao;
import io.renren.modules.question.entity.CommonQuestionEntity;
import io.renren.modules.question.service.CommonQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("commonquestion")
@Api(tags = "常见问题管理")
public class CommonQuestionController {

    @Autowired
    private CommonQuestionService commonQuestionService;

    @Autowired
    private CommonQuestionDao commonQuestionDao;

    @RequestMapping("list")
    @ApiOperation(value = "查询列表")
    public R findList(@Param("param") Map<String, Object> params) {
        PageUtils page = commonQuestionService.queryPage(params);
        return R.ok().put("page", page);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "批量删除")
    public R delete(@RequestBody Integer[] ids) {
        if (ids == null) {
            return R.error("未选择问题id");
        }
        boolean result = commonQuestionService.removeByIds(Arrays.asList(ids));
        if (!result) {
            return R.error();
        }
        return R.ok();
    }

    @GetMapping("info/{id}")
    @ApiOperation(value = "查询详细信息")
    public R info(@PathVariable("id") Integer id) {
        if (id != null) {
            CommonQuestionEntity entity = commonQuestionService.getById(id);
            return R.ok().put("commonQuestion", entity);
        }
        return R.error();
    }

    @PostMapping("saveOrUpdate")
    @ApiOperation(value = "新增或修改")
    public R saveOrUpdate(@RequestBody CommonQuestionEntity entity) {
        if (entity != null) {
            //新增
            if (entity.getId() == null) {
                entity.setCreateTime(new Date());
            }
            entity.setUpdateTime(new Date());
            boolean result = commonQuestionService.saveOrUpdate(entity);
            return R.ok();
        }
        return R.error();
    }
}
